﻿Imports BUS
Imports DTO
Imports System.Data.Linq

Public Class MH_Chinh
    'Reset
    Private Sub Reset()
        TH_Mathe.Text = ""
        TH_Mathe2.Text = ""
        TH_Hoten.Text = ""
        TH_Hoten2.Text = ""
        TH_Cmnd.Text = ""
        TH_Cmnd2.Text = ""
        TH_Diachi.Text = ""
        TH_Diachi2.Text = ""
        TH_Tinhtrangdocgia.Text = ""
        TH_Tinhtrangdocgia2.Text = ""
        TH_Tinhtrangsach.Text = ""
        TH_Masach.Text = ""
        TH_Tacgia.Text = ""
        TH_Tuasach.Text = ""
        TH_Nxb.Text = ""
        TH_Tonkho.Text = ""
        btn_Muon.Enabled = False
        btn_Tra.Enabled = False
        Temp.Text = ""
    End Sub

    'Kiem tra chuoi nhap co phai so
    Private Function Kiemtra_nhapint(ByVal s As String) As String
        Dim Kq As String = ""
        Dim Hop_le As Boolean
        Dim t As Integer

        Hop_le = Integer.TryParse(s, t)
        If Not Hop_le Then
            Kq &= "Cần nhập số" & vbCrLf
        End If

        Return Kq
    End Function

    'Goi man hinh About
    Private Sub MN_TG_About_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MN_TG_About.Click
        Dim MH As New MH_About
        MH.Show()
    End Sub

    'Goi man hinh Quan ly Doc gia
    Private Sub MN_QL_Docgia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MN_QL_Docgia.Click
        Dim MH As New MH_Quanlydocgia
        MH.Show()
    End Sub

    'Goi man hinh Quan ly Sach
    Private Sub MN_QL_Sach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MN_QL_Sach.Click
        Dim MH As New MH_Quanlysach
        MH.Show()
    End Sub

    'Tra cuu sach
    Private Sub btn_Tracuusach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Tracuusach.Click
        TH_Trangthaitracuusach.Text = "Đang tra cứu ..."
        TH_Trangthaitracuusach.Update()

        Dim output = "OK"

        If TH_Tieuchitimsach.Text = "" Then
            'Lay het bang du lieu
            TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu()
        Else
            'Lay theo tieu chi tim kiem
            Select Case TH_Chontieuchitimsach.SelectedIndex
                Case 0
                    output = Kiemtra_nhapint(TH_Tieuchitimsach.Text)
                    If output = "" Then
                        TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu_Theomasach(TH_Tieuchitimsach.Text)
                    End If
                Case 1
                    TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu_Theotuasach(TH_Tieuchitimsach.Text)
                Case 2
                    TH_DS_Sach.DataSource = SachBus.LayBang_Tracuu_Theotacgia(TH_Tieuchitimsach.Text)
            End Select
        End If

        TH_Trangthaitracuusach.Text = output
    End Sub

    'Load
    Private Sub MH_Chinh_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TH_Chontieuchitimsach.SelectedIndex = 0
    End Sub

    'Kiem tra doc gia co du dieu kien muon khong
    Private Sub btn_Kiemdocgia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Kiemdocgia.Click
        Dim chuoiloi = Kiemtra_nhapint(TH_Mathe.Text)
        If chuoiloi = "" Then
            TH_Tinhtrangdocgia.Text = "Đang kiểm tra"
            TH_Tinhtrangdocgia.Refresh()

            Dim the = TheThuVienBus.LayThe(TH_Mathe.Text)
            If the.MT <> -1 Then
                'The co ton tai
                Dim docgia = DocGiaBus.LayDocGia(the.MDG)
                TH_Hoten.Text = docgia.Ho_ten
                TH_Cmnd.Text = docgia.Cmnd
                TH_Diachi.Text = docgia.Dia_chi
                TH_Tinhtrangdocgia.Text = "Kiểm xong" & vbCrLf
                'Xem the het han chua
                If the.Ngay_het_han < Date.Now() Then
                    TH_Tinhtrangdocgia.Text &= "Thẻ hết hạn" & vbCrLf
                    btn_Muon.Enabled = False
                Else
                    TH_Tinhtrangdocgia.Text &= "Thẻ còn hạn" & vbCrLf
                    'Co no sach chua tra khong
                    Dim phieumuoncuoi = PhieuMuonBus.LayPhieuMuon(the.MT)
                    If phieumuoncuoi.MPM <> -1 Then
                        Dim chitietmuon = ChiTietMuonTraBus.LayChiTiet(phieumuoncuoi.MPM)
                        If chitietmuon.MPT > 0 Then
                            TH_Tinhtrangdocgia.Text &= "Không nợ sách" & vbCrLf
                            btn_Muon.Enabled = True
                        Else
                            TH_Tinhtrangdocgia.Text &= "Lần trước mượn chưa trả" & vbCrLf
                            btn_Muon.Enabled = False
                        End If
                    Else
                        TH_Tinhtrangdocgia.Text &= "Chưa mượn bao giờ" & vbCrLf
                        btn_Muon.Enabled = True
                    End If
                End If
            Else
                'The khong ton tai
                TH_Tinhtrangdocgia.Text = "Kiểm xong" & vbCrLf
                TH_Tinhtrangdocgia.Text &= "Thẻ không tồn tại" & vbCrLf
                btn_Muon.Enabled = False
            End If
            TH_Tinhtrangdocgia.Refresh()
        End If
    End Sub

    'Kiem tra sach co the muon khong
    Private Sub btn_Kiemsach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Kiemsach.Click
        Dim chuoiloi = Kiemtra_nhapint(TH_Masach.Text)
        If chuoiloi = "" Then
            TH_Tinhtrangsach.Text = "Đang kiểm tra"
            TH_Tinhtrangsach.Refresh()

            Dim sch = SachBus.LaySach(TH_Masach.Text)
            If sch.MS <> -1 Then
                'Sach co ton tai
                Dim nganchua = KhoBus.LayNganChua(sch.MS)
                TH_Tacgia.Text = sch.Tac_gia
                TH_Tuasach.Text = sch.Tua_sach
                TH_Nxb.Text = sch.Nha_xuat_ban
                TH_Tonkho.Text = nganchua.Ton_kho
                TH_Tinhtrangsach.Text = "Kiểm xong" & vbCrLf
                'Xem con sach khong
                If nganchua.Ton_kho <= 0 Then
                    TH_Tinhtrangsach.Text &= "Sách đã được mượn hết" & vbCrLf
                    btn_Muon.Enabled = False
                Else
                    TH_Tinhtrangsach.Text &= "Còn sách để cho mượn" & vbCrLf
                    btn_Muon.Enabled = True
                End If
            Else
                'Sach khong ton tai
                TH_Tinhtrangsach.Text = "Kiểm xong" & vbCrLf
                TH_Tinhtrangsach.Text &= "Sách không tồn tại" & vbCrLf
                btn_Muon.Enabled = False
            End If
            If TH_Mathe.Text = "" Then
                btn_Muon.Enabled = False
            End If
            TH_Tinhtrangsach.Refresh()
        End If
    End Sub

    'Muon sach
    Private Sub btn_Muon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Muon.Click
        Dim phieumuon As New PhieuMuonSach
        Dim chitiet As New ChiTietMuonTra
        Dim nganchua = KhoBus.LayNganChua(TH_Masach.Text)
        nganchua.Ton_kho = nganchua.Ton_kho - 1
        phieumuon.Ngay_muon = TH_Ngaymuon.Value
        phieumuon.MT = TH_Mathe.Text
        chitiet.MS = TH_Masach.Text
        KhoBus.CapNhat_muontra(nganchua)
        PhieuMuonBus.ThemChiTiet(phieumuon, chitiet)
        Reset()
    End Sub

    'Kiem tra co du dieu kien tra khong
    Private Sub btn_Kiemdocgia2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Kiemdocgia2.Click
        Temp.Text = ""
        Dim chuoiloi = Kiemtra_nhapint(TH_Mathe2.Text)
        If chuoiloi = "" Then
            TH_Tinhtrangdocgia2.Text = "Đang kiểm tra"
            TH_Tinhtrangdocgia2.Refresh()

            Dim the = TheThuVienBus.LayThe(TH_Mathe2.Text)
            If the.MT <> -1 Then
                'The co ton tai
                Dim docgia = DocGiaBus.LayDocGia(the.MDG)
                TH_Hoten2.Text = docgia.Ho_ten
                TH_Cmnd2.Text = docgia.Cmnd
                TH_Diachi2.Text = docgia.Dia_chi
                TH_Tinhtrangdocgia2.Text = "Kiểm xong" & vbCrLf
                'Co no sach chua tra khong
                Dim phieumuoncuoi = PhieuMuonBus.LayPhieuMuon(the.MT)
                If phieumuoncuoi.MPM <> -1 Then
                    Dim chitietmuon = ChiTietMuonTraBus.LayChiTiet(phieumuoncuoi.MPM)
                    If chitietmuon.MPT > 0 Then
                        TH_Tinhtrangdocgia2.Text &= "Không nợ sách" & vbCrLf
                        btn_Tra.Enabled = False
                    Else
                        TH_Tinhtrangdocgia2.Text &= "Lần trước mượn chưa trả" & vbCrLf
                        Temp.Text = chitietmuon.MS
                        btn_Tra.Enabled = True
                    End If
                Else
                    TH_Tinhtrangdocgia2.Text &= "Không nợ sách" & vbCrLf
                    btn_Tra.Enabled = False
                End If

            Else
                'The khong ton tai
                TH_Tinhtrangdocgia.Text = "Kiểm xong" & vbCrLf
                TH_Tinhtrangdocgia.Text &= "Thẻ không tồn tại" & vbCrLf
                btn_Muon.Enabled = False
            End If
            TH_Tinhtrangdocgia.Refresh()
        End If
    End Sub

    'Tra sach
    Private Sub btn_Tra_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Tra.Click
        Dim phieumuon = PhieuMuonBus.LayPhieuMuon(TH_Mathe2.Text)
        Dim nganchua = KhoBus.LayNganChua(Temp.Text)
        Dim phieutra As New PhieuTraSach

        phieutra.Ngay_tra = TH_Ngaytra.Value
        phieutra.MT = phieumuon.MT
        nganchua.Ton_kho = nganchua.Ton_kho + 1

        PhieuTraBus.Them(phieutra, phieumuon)
        KhoBus.CapNhat_muontra(nganchua)
        Reset()
    End Sub

    Private Sub TH_Mathe_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TH_Mathe.KeyDown
        If e.KeyValue = Keys.Enter Then
            btn_Kiemdocgia.PerformClick()
        End If
    End Sub

    Private Sub TH_Tieuchitimsach_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TH_Tieuchitimsach.KeyDown
        If e.KeyValue = Keys.Enter Then
            btn_Tracuusach.PerformClick()
        End If
    End Sub

    Private Sub TH_Mathe_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TH_Mathe.Enter
        btn_Muon.Enabled = False
    End Sub

    Private Sub TH_Masach_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TH_Masach.Enter
        btn_Muon.Enabled = False
    End Sub

    Private Sub TH_Mathe2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TH_Mathe2.KeyDown
        If e.KeyValue = Keys.Enter Then
            btn_Kiemdocgia2.PerformClick()
        End If
    End Sub

    Private Sub TH_Mathe2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TH_Mathe2.Enter
        btn_Tra.Enabled = False
    End Sub
End Class
